package com.Expression;


public class ArrayStack2 {
    private int maxSize;
    private int stack[]; //用数组模拟栈，
    private int top = -1;

    public ArrayStack2(int maxSize){
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }


    /**
     * 判断栈满top ? maxSize -1
     */
    public boolean isFull(){
        return top == maxSize -1 ;
    }

    /**
     * 判断栈空：top = -1
     */
    public boolean isEmpty(){
        return top == -1;
    }
    /**
     * 入栈
     */
    public void push(int value){
        //判断栈是否已满
        if(isFull()){
            System.out.println("堆栈已满");
            return;
        }
        top++;
        stack[top]=value;
    }

    /**
     * 出栈：先获取栈顶元素，再移动栈顶指针。
     */
    public int pop(){
        //判断栈是否为空
        if(isEmpty()){
            throw new RuntimeException("栈为空，无法出栈");
        }
        int value = stack[top];
        top--;
        return value;
    }

    /**
     * 遍历栈：从栈顶显示数据
     */
    public void list(){
        if(isEmpty()){
            throw new RuntimeException("栈为空");
        }
        for(int temp = top;temp > -1;temp--){
            System.out.println("stack["+temp+"] = "+stack[temp]);
        }
    }
    /**
     * 返回运算符优先级，优先级用数字来表示，数字越大表示优先级越高
     */
    public int priority(int oper){
        if(oper == '*' || oper == '/'){
            return 1;
        }else if(oper == '+' || oper == '-'){
            return 0;
        }else {
            return -1;
        }
    }

    public boolean isOper(char val){
        return priority(val)!=-1;
    }
    public int cal(int num1,int num2,int oper){
        int result = 0;
        switch (oper){
            case '+':
                result = num2 + num1;
                break;
            case '-':
                //注意顺序
                result = num2 - num1;
                break;
            case '*':
                result = num2 * num1;
                break;
            case '/':
                //注意顺序
                result = num2 / num1;
                break;
            default:
                break;
        }
        return result;
    }

    /**
     * 查看栈顶元素
     */
    public int peek(){
        return stack[top];
    }

}
